<?php

/**
 * Description of Article Controller
 *
 * @author Vu
 */
class Admin_Cms_ArticleController extends My_Controller_Action {
	public function init(){
		parent::init();
		
		$auth = Zend_Auth::getInstance();
        
        if(!$auth->hasIdentity())
        { 
            $this->_redirect("user/account/login");
        }
        
        if ($this->isAccess($auth->getIdentity()->group_id, 2))
        {
			//to do
		} else {
            die("Access denied");
        }
	}   
	
    public function indexAction() {
        $this->view->rightAdminModules('right');
        
        $this->view->by = $this->_request->getParam("by", "created");
        $this->view->order = $this->_request->getParam("order", "desc");

        Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
        $model = $this->loadModel('admin_cms', 'Article');
        $this->view->paginator = $model->getAllArticles($this->_getParam('page', 1), $this->view->by.' '.$this->view->order);
    }

    public function addAction() {
        
        $this->view->rightAdminModules('right');

        $this->view->CKEditorScript = $this->view->setupCKEditor('content');
        $this->view->errors = $this->_errorMessenger->getMessages();
        $this->_errorMessenger->clearMessages();
    }

    public function addpostAction() {
        $model = $this->loadModel('admin_cms', 'Article');
        $newarticle = $model->createrow();
        if ($this->_request->getParam('title')) {
            $newarticle->title = $this->_request->getParam('title');
            $newarticle->content = $this->_request->getParam('content');
            $newarticle->save();

            $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "article", "action" => "index"));
            $this->_redirect($redirect_url);
        } else {
            $this->_errorMessenger->addMessage($this->view->translate("Please write article title"));
            $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "article", "action" => "add"));
            $this->_redirect($redirect_url);
        }
    }

    public function deleteAction() {
        $model = $this->loadModel('admin_cms', 'Article');
        $articleid = $this->_request->getParam('id');
        $article = $model->getArticleById($articleid);
        $article->delete();

        $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "article", "action" => "index"));
        $this->_redirect($redirect_url);
    }

    public function editAction() {
        $this->view->rightAdminModules('right');

        $this->view->errors = $this->_errorMessenger->getMessages();
        $this->_errorMessenger->clearMessages();

        $model = $this->loadModel('admin_cms', 'Article');
        $articleid = $this->_request->getParam('id');
        $this->view->article = $model->getArticleById($articleid);
    }

    public function editpostAction() {
        if ($this->_request->isPost()) {
            if ($this->_request->getParam('title')) {
                $model = $this->loadModel('admin_cms', 'Article');
                $articleid = $this->_request->getParam('article_id');

                $article = $model->getArticleById($articleid);
                $article->title = $this->_request->getParam('title');
                $article->content = $this->_request->getParam('content');
                $article->save();

                $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "article", "action" => "index"));
                $this->_redirect($redirect_url);
            } else {
                $this->_errorMessenger->addMessage($this->view->translate("Please write article title"));
                $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "article", "action" => "edit", "id" => $articleid));
                $this->_redirect($redirect_url);
            }
        } else {
            $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "article", "action" => "index"));
            $this->_redirect($redirect_url);
        }
    }

}
